package com.example.timer;

import com.example.pojo.Result;
import com.example.pojo.goodproductQuestionlistPojo;
import com.example.pojo.measuringToolChecklistPojo;
import com.example.pojo.measuringToolsListPojo;
import com.example.service.goodproductQuestionlistService;
import com.example.service.measuringToolChecklistService;
import com.example.service.measuringToolsListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * ClassName:goodProductConditionTimer
 * Package:com.example.timer
 * Description:
 *
 * @Author：牧星苑
 * @Create 2025/7/3 15:32
 * @Version 1.0
 */
@Component
public class goodProductConditionTimer {
    @Autowired
    private measuringToolsListService measuringToolsListService;
    @Autowired
    private measuringToolChecklistService measuringToolChecklistService;
    @Autowired
    private goodproductQuestionlistService goodproductQuestionlistService;
    //早班定时执行
    @Scheduled(cron = "0 30 22 * * ?")
    public void monitorMorningMeasuringToolChecklist() {
        //把品管科的量检具基本信息表调出
        List<measuringToolsListPojo> measuringToolsListPojos=measuringToolsListService.queryAll();
        //定义早班点检需要统计的数据
        int isCheckMorning=0;
        int noCheckMorning=0;
        int allCheckMorning=0;
        String messageCheckMorning="";

        //定义周点检需要统计的数据
        int isCheckWeek=0;//记录检查发现每周点检的情况：实际点检多少；
        int noCheckWeek=0;//记录检查发现每周点检的情况：未点检多少；
        int allCheckWeek=0;//记录检查发现每周点检的情况：应点检多少；
        String messageCheckWeek="";
        for (int i = 0; i < measuringToolsListPojos.size(); i++) {
            //逐条比对，需要每班点检的
            if (measuringToolsListPojos.get(i).getInspection_frequency().equals("1shift")){
                allCheckMorning=allCheckMorning+1;
                LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(13,0,0));
                LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(22,30,0));
                String numberUse=measuringToolsListPojos.get(i).getNumber();
                List<measuringToolChecklistPojo> measuringToolChecklistPojos = measuringToolChecklistService.queryTimeAndNumber(startTime, endTime, numberUse);
                if (measuringToolChecklistPojos.size()>0){
                    isCheckMorning=isCheckMorning+1;
                }
                else {
                    noCheckMorning=noCheckMorning+1;
                    if (messageCheckMorning==""){
                        messageCheckMorning=measuringToolsListPojos.get(i).getNumber();
                    }
                    else {
                        messageCheckMorning=messageCheckMorning+";"+measuringToolsListPojos.get(i).getNumber();
                    }
                }
            }
            //逐条比对，需要每周点检的
            else if (measuringToolsListPojos.get(i).getInspection_frequency().equals("1week")){
                allCheckWeek=allCheckWeek+1;
                LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(13, 0, 0));
                LocalDateTime startTime = endTime.minusDays(8);
                endTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(22, 30, 0));
                String numberUse=measuringToolsListPojos.get(i).getNumber();
                List<measuringToolChecklistPojo> measuringToolChecklistPojos = measuringToolChecklistService.queryTimeAndNumber(startTime, endTime, numberUse);
                if (measuringToolChecklistPojos.size()>0){
                    isCheckWeek=isCheckWeek+1;
                }
                else {
                    noCheckWeek=noCheckWeek+1;
                    if (messageCheckWeek==""){
                        messageCheckWeek=measuringToolsListPojos.get(i).getNumber();
                    }
                    else {
                        messageCheckWeek=messageCheckWeek+";"+measuringToolsListPojos.get(i).getNumber();
                    }
                }
            }
        }
        if (noCheckMorning > 0){
            goodproductQuestionlistPojo Question = new goodproductQuestionlistPojo();//周、日；
            //日：向数据库中写入问题清单，组合数据
            Question.setQuestion_classification("执行问题");
            Question.setQuestion_source("良品条件");
            Question.setSource_classification("品管-量检具");
            Question.setDepartment("品管/Quality Management/Управления качеством");
            String getQuestion_description1
                    ="未按时进行量检具早班点检，本次应点检："
                    +String.valueOf(allCheckMorning)
                    +",未点检"
                    +String.valueOf(noCheckMorning)
                    +",未点检量检具编号为："
                    +messageCheckMorning;
            Question.setQuestion_description(getQuestion_description1);
            Question.setGeneration_time(LocalDate.now().toString()); // 默认格式就是 yyyy-MM-dd
            //向数据库中写入问题清单，写入数据
            goodproductQuestionlistService.insertOne(Question);
        }

        if (noCheckWeek > 0) {
            goodproductQuestionlistPojo Question = new goodproductQuestionlistPojo();
            //周：向数据库中写入问题清单，组合数据
            Question.setQuestion_classification("执行问题");
            Question.setQuestion_source("良品条件");
            Question.setSource_classification("品管-量检具");
            Question.setDepartment("品管/Quality Management/Управления качеством");
            String getQuestion_description
                    ="未按时进行量检具周点检，本次应点检："
                    +String.valueOf(allCheckWeek)
                    +",未点检"
                    +String.valueOf(noCheckWeek)
                    +",未点检量检具编号为："
                    +messageCheckWeek;
            Question.setQuestion_description(getQuestion_description);
            Question.setGeneration_time(LocalDate.now().toString()); // 默认格式就是 yyyy-MM-dd
            //向数据库中写入问题清单，写入数据
            goodproductQuestionlistService.insertOne(Question);
        }
    }
    //中班定时执行
    @Scheduled(cron = "0 59 17 * * ?")
    public void monitorMiddleMeasuringToolChecklist() {
        //把品管科的量检具基本信息表调出
        List<measuringToolsListPojo> measuringToolsListPojos=measuringToolsListService.queryAll();
        //定义中班点检需要统计的数据
        int isCheckMiddle=0;
        int noCheckMiddle=0;
        int allCheckMiddle=0;
        String messageCheckMiddle="";
        for (int i = 0; i < measuringToolsListPojos.size(); i++) {
            //逐条比对，需要每班点检的
            if (measuringToolsListPojos.get(i).getInspection_frequency().equals("1shift")){
                allCheckMiddle=allCheckMiddle+1;
                LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(22, 0, 0));
                LocalDateTime endTime = LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.of(7, 30, 0));
                String numberUse=measuringToolsListPojos.get(i).getNumber();
                List<measuringToolChecklistPojo> measuringToolChecklistPojos = measuringToolChecklistService.queryTimeAndNumber(startTime, endTime, numberUse);
                if (measuringToolChecklistPojos.size()>0){
                    isCheckMiddle=isCheckMiddle+1;
                }
                else {
                    noCheckMiddle=noCheckMiddle+1;
                    if (messageCheckMiddle==""){
                        messageCheckMiddle=measuringToolsListPojos.get(i).getNumber();
                    }
                    else {
                        messageCheckMiddle=messageCheckMiddle+";"+measuringToolsListPojos.get(i).getNumber();
                    }
                }
            }
        }
        System.out.println(allCheckMiddle);
        if (noCheckMiddle > 0){
            goodproductQuestionlistPojo Question = new goodproductQuestionlistPojo();
            //日：向数据库中写入问题清单，组合数据
            Question.setQuestion_classification("执行问题");
            Question.setQuestion_source("良品条件");
            Question.setSource_classification("品管-量检具");
            Question.setDepartment("品管/Quality Management/Управления качеством");
            String getQuestion_description2
                    ="未按时进行量检具中班点检，本次应点检："
                    +String.valueOf(allCheckMiddle)
                    +",未点检"
                    +String.valueOf(noCheckMiddle)
                    +",未点检量检具编号为："
                    +messageCheckMiddle;
            Question.setQuestion_description(getQuestion_description2);
            Question.setGeneration_time(LocalDate.now().toString()); // 默认格式就是 yyyy-MM-dd
            //向数据库中写入问题清单，写入数据
            goodproductQuestionlistService.insertOne(Question);
        }
    }
    //
}
